package thread.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
//测试map的线程安全
public class MapTest {
	static void doit() throws Exception {
		final int count = 200;
		final AtomicInteger checkNum = new AtomicInteger(0);
		ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(100);
		//
		final Map<Long, String> map = new WeakHashMap<Long, String>();
		map.put(0L, "www.imxylz.cn");
		// map.put(1L, "www.imxylz.cn");
		for (int j = 0; j < count; j++) {
			newFixedThreadPool.submit(new Runnable() {
				public void run() {
					map.put(System.nanoTime() + new Random().nextLong(), "www.imxylz.cn");
					String obj = map.get(0L);
					if (obj == null) {
						checkNum.incrementAndGet();
					}
				}
			});
		}
		newFixedThreadPool.awaitTermination(1, TimeUnit.SECONDS);
		newFixedThreadPool.shutdown();

		System.out.println(checkNum.get());
	}

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 10; i++) {
			doit();
			Thread.sleep(500L);
		}
	}
}
